﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.ApplicationPages;
using System.Web.UI.WebControls;
using Winwise.SPWebConfig.Core;
using System.Collections.Generic;
using Winwise.SPWebConfig.Core.XPathXmlModification;

namespace Winwise.SPWebConfig.UI
{
    public partial class AddEditXPathXmlWebconfigModification : WebConfigDialogAdminPageBase
    {
        private void DisplayHide()
        {
            SectionNodeOptions.Visible = RBLType.SelectedIndex == 0;
            SectionNodePosition.Visible = RBLType.SelectedIndex == 0;
            SectionNodeRelativeSiblingXPath.Visible = RBNodeType.SelectedIndex == 2 || RBNodeType.SelectedIndex == 3;

        }

       public  void RBLType_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayHide();
        }

       public void RBNodeType_SelectedIndexChanged(object sender, EventArgs e)
       {
           DisplayHide();
       }


       public void ApplyWebConfigModif(object sender, EventArgs e)
        {
            ApplyWebConfigModif(this.Selector.CurrentItem);
        }


        public void OnContextChange(object sender, EventArgs e)
        {
            if (this.Selector.CurrentItem != null)
            {
                SPWebApplication currentItem = this.Selector.CurrentItem;
                OnContextChange(currentItem);
                LinkManage.HRef = GetManageLink(currentItem);
                DisplayHide();
            }
        }

     
             
        public void AddModif(object sender, EventArgs e)
        {
            AddEditModif<XPathXmlWebConfigModification>(this.Selector.CurrentItem);
        }

        public override void FillWebConfigModif(WebConfigModification modif)
        {
            XPathXmlWebConfigModification modifXpath = modif as XPathXmlWebConfigModification;
            modifXpath.DisplayName = TBDisplayName.Text;

            modifXpath.Zones.Clear();
            foreach (ListItem checkbox in CheckBoxZones.Items)
            {
                if (checkbox.Selected)
                {
                    modifXpath.Zones.Add((SPUrlZone)Enum.Parse(typeof(SPUrlZone), checkbox.Text));
                }
            }

            modifXpath.ParentXPath = TBParentXPath.Text;
            modifXpath.RelativeXPath = TBRelativeXPath.Text;
            modifXpath.ModificationType = (XPathXmlWebConfigModificationType)RBLType.SelectedIndex;
            modifXpath.Value = TBValue.Text;
            modifXpath.XPathInsertPosition = (XPathXmlInsertPosition)RBNodeType.SelectedIndex;
            modifXpath.RelativeSiblingXPath = TBRelativeSiblingXPath.Text;
            modifXpath.EraseNodeOnRemove = CBEraseNodeOnRemove.Checked;
        }

        public override void DataBind(WebConfigModification modif)
        {
            XPathXmlWebConfigModification modifXPathXml = modif as XPathXmlWebConfigModification;
            TBDisplayName.Text = modifXPathXml.DisplayName;
            TBParentXPath.Text = modifXPathXml.ParentXPath;
            TBRelativeXPath.Text = modifXPathXml.RelativeXPath;

            switch (modifXPathXml.ModificationType)
            {
                case XPathXmlWebConfigModificationType.AddNode:
                    RBLType.SelectedIndex = 0;
                    break;
                case XPathXmlWebConfigModificationType.ModifyAttribute:
                    RBLType.SelectedIndex = 1;
                    break;
            }

            TBValue.Text = modifXPathXml.Value;
            switch (modifXPathXml.XPathInsertPosition)
            {
                case XPathXmlInsertPosition.Last:
                    RBNodeType.SelectedIndex = 0;
                    break;
                case XPathXmlInsertPosition.First:
                    RBNodeType.SelectedIndex = 1;
                    break;
                case XPathXmlInsertPosition.Before:
                    RBNodeType.SelectedIndex = 2;
                    break;
                case XPathXmlInsertPosition.After:
                    RBNodeType.SelectedIndex = 3;
                    break;
            }

            TBRelativeSiblingXPath.Text = modifXPathXml.RelativeSiblingXPath;
            CBEraseNodeOnRemove.Checked = modifXPathXml.EraseNodeOnRemove;
            
            CheckBoxZones.ClearSelection();
            foreach (var zone in modifXPathXml.Zones)
            {
                CheckBoxZones.Items.FindByText(zone.ToString()).Selected = true;
            }
        }

        public override void UpdateMessage(string message)
        {
            LblResults.Text = message;
        }

        public override void UpdateErrorMessage(string message)
        {
            LabelErrorMessage.Text = message;
        }
    }
}
